/****************************************************************************
@file    : aircon1_McQuar.c
@date    : 2019-03-09
@brief   : 麦克维尔空调遥控
*****************************************************************************/

/* Includes ----------------------------------------------------------------*/
#include "aircon.h"
#include "string.h"
/* Private define ----------------------------------------------------------*/
#define  LEADCODE_TIME_H		(uint16_t)( 0x8000   | (uint16_t)4700 )
#define  LEADCODE_TIME_L		(uint16_t)( (~0x8000)& (uint16_t)2400 )
#define  ENDCODE_TIME_H			(uint16_t)( 0x8000   | (uint16_t)400 )
#define  ENDCODE_TIME_L	    	(uint16_t)( (~0x8000)& (uint16_t)20000)
#define  ENDCODE_TIME2_H	    (uint16_t)( 0x8000   | (uint16_t)4500 )
#define  ENDCODE_TIME2_L	    (uint16_t)( (~0x8000)& (uint16_t)20000)

#define  McQuar_BIT0_TIME_H			(uint16_t)( 0x8000   | (uint16_t)400 )
#define  McQuar_BIT0_TIME_L			(uint16_t)( (~0x8000)& (uint16_t)320 )
#define  McQuar_BIT1_TIME_H			(uint16_t)( 0x8000   | (uint16_t)400 )
#define  McQuar_BIT1_TIME_L			(uint16_t)( (~0x8000)& (uint16_t)900 )

///
//DATA0~DATA1 地址码,固定
#define DATA0					0x0
#define DATA1					0x1

//DATA2 模式
#define DATA2_MODE_COOL			0x2
#define DATA2_MODE_HEAT			0x8
#define DATA2_MODE_WENT			0x4
#define DATA2_MODE_DRY			0x1
#define DATA2_MODE_AUTO			0xA

//DATA3 风速
#define DATA3_SPEED_LOW			0x8
#define DATA3_SPEED_MID			0x4
#define DATA3_SPEED_HIGH		0x2
#define DATA3_SPEED_AUTO		0x1


//DATA4 ~ DATA12  预留 0x0

//DATA12 设定温度 16 ~ 30 个位
//DATA13 设定温度 16 ~ 30 十位

//DATA14 开关机
#define DATA14_ONOFF_ON			0x8
#define DATA14_ONOFF_OFF		0x0

//DATA14 DATA0~14和校验


/* Private variables -------------------------------------------------------*/
static uint16_t McQuarSendlen = 0;                //发送帧组包长度

/* Private function prototypes ---------------------------------------------*/
static void McQuarPackageByte(uint8_t byte);

/* Private functions -------------------------------------------------------*/
static void McQuarPackageByte(uint8_t byte) {
	uint8_t i;

	for(i = 0; i < 4; i++) {
		if(byte&(0x01<<(i))) {	//高电平
			IrSendBuf[McQuarSendlen++] = McQuar_BIT1_TIME_H;
			IrSendBuf[McQuarSendlen++] = McQuar_BIT1_TIME_L;
		} else {				//低电平
			IrSendBuf[McQuarSendlen++] = McQuar_BIT0_TIME_H;
			IrSendBuf[McQuarSendlen++] = McQuar_BIT0_TIME_L;
		}
	}
}

/* Public functions --------------------------------------------------------*/
void McQuarIrPackage(void *p) {
	uint8_t i;
	uint8_t data[16] = {DATA0,DATA1,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

	uint8_t AirConOnOff = ((uint8_t*)p)[0];
	uint8_t AirConMode = ((uint8_t*)p)[1];
	uint8_t AirConFan = ((uint8_t*)p)[2];
	uint8_t AirConTmp = ((uint8_t*)p)[3];

	//设定温度
	if(AirConTmp > 30)  AirConTmp = 30;
	else if(AirConTmp < 16)  AirConTmp = 16;

	
		//开关
	if(AirConOnOff == AIRCON_ON) {
		data[14] = DATA14_ONOFF_ON;
	} else {
		data[14] = DATA14_ONOFF_OFF;
	}

	//模式
	switch(AirConMode) {
	case AIRCON_COOLING:
		data[2] = DATA2_MODE_COOL;
		break;
	case AIRCON_HEATING:
		data[2] = DATA2_MODE_HEAT;
		break;
	case AIRCON_WENTING:
		data[2] = DATA2_MODE_WENT;
		break;
	default:
		break;
	}

	//风速
	switch(AirConFan) {
	case AIRCON_WINDLOW:
		data[3] = DATA3_SPEED_LOW;
		break;
	case AIRCON_WINDMID:
		data[3] = DATA3_SPEED_MID;
		break;
	case AIRCON_WINDHIG:
		data[3] = DATA3_SPEED_HIGH;
		break;
	case AIRCON_WINDAUTO:
		data[3] = DATA3_SPEED_AUTO;
		break;
	default:
		break;
	}

	//设置温度
	data[12] = AirConTmp%10;
	data[13] = AirConTmp/10;
	
	//校验
	for(i = 0; i < 15; ++i) {
		data[15] += data[i];
	}

	data[15] &= 0x0F;

	
	//while(IrDev.State != IRREMOTE_IDLE);  //等待发送完成
	
	memset(IrSendBuf, 0, sizeof(IrSendBuf)/sizeof(uint16_t));
	
	McQuarSendlen = 0;
	
	
	IrSendBuf[McQuarSendlen++] = LEADCODE_TIME_H;
	IrSendBuf[McQuarSendlen++] = LEADCODE_TIME_L;
	
	for(i = 0; i < 16; i++) {
		McQuarPackageByte(data[i]);
	}

	IrSendBuf[McQuarSendlen++] = ENDCODE_TIME_H;
	IrSendBuf[McQuarSendlen++] = ENDCODE_TIME_L;
	IrSendBuf[McQuarSendlen++] = ENDCODE_TIME2_H;
	IrSendBuf[McQuarSendlen++] = ENDCODE_TIME2_L;
	IrSendBuf[McQuarSendlen++] = ENDCODE_TIME2_L;
	IrSendBuf[McQuarSendlen++] = ENDCODE_TIME2_L;
	IrSendBuf[McQuarSendlen++] = ENDCODE_TIME2_L;
	IrSendBuf[McQuarSendlen++] = ENDCODE_TIME2_L;
	//组包完成提交
	IrDev.Ptr 		= IrSendBuf;
	IrDev.Len 		= McQuarSendlen;
	IrDev.State 	= IRREMOTE_REQUIRE;
}
